﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using _MetalSlug.Soldiers.Units;
using _MetalSlug.Form.ScreenSystem;
using _MetalSlug.Form;
using _MetalSlug.Form.ScreenSystem.Message;

namespace _MetalSlug.Weapons.Units
{
    public class ShotGunBullet : Bullet
    {
        public ShotGunBullet()
        {
        }

        public ShotGunBullet(Vector2 topleft, Vector2 size, Vector2 _Speed)
        {
            Type = TYPE_BULLET.SHOTGUN_BULLET;
            InitialAttribute(topleft, size, _Speed);
        }

        public override void LoadContent()
        {
            List<Texture2D> Textures = GameUtil.LoadTextures("Weapon/ShotGunBullet/", 1);
            MySprite temp = new MySprite(Textures, TopLeft);
            temp.Size = Size;
            Sprites.Add(temp);
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            Sprites[0].TopLeft = TopLeft;
            if (Visible == true)
            {
                base.Draw(spriteBatch);
            }
        }

        public override void Fire(Vector2 theStartPosition, Vector2 theDirection)
        {
            if (theDirection == Vector2.UnitX)
            {
                TopLeft = new Vector2(theStartPosition.X + 120, theStartPosition.Y + 40);
                Sprites[0].Flip = MySprite.FLIPABLE.NOT_FLIP;
            }
            else
            {
                TopLeft = new Vector2(theStartPosition.X - 50, theStartPosition.Y + 40);
                Sprites[0].Flip = MySprite.FLIPABLE.FLIP;
            }

            StartPosition = theStartPosition;
            Direction = theDirection;
        }

        public override void Update(GameTime gameTime, List<Enemy> enemies)
        {
            foreach (Enemy enemy in enemies)
            {
                if (!enemy.IsDead)
                {
                    if ((TopLeft.X >= enemy.TopLeft.X) && (TopLeft.X <= enemy.TopLeft.X + 100) &&
                        (TopLeft.Y >= enemy.TopLeft.Y) && (TopLeft.Y <= enemy.TopLeft.Y + 180))
                    {
                        enemy.Hp -= 50;
                        MyMessage sendMessage = new AddGameCoreMessage(10);
                        GameManager.GetInstance().Receive(null,MyScreen.TYPE.PLAYGAME, sendMessage);
                        Visible = false;
                        break;
                    }
                }
            }

            if (Vector2.Distance(StartPosition, TopLeft) > 800)
            {
                Visible = false;
            }

            if (Visible == true)
            {
                TopLeft += Direction * Speed * (float)gameTime.ElapsedGameTime.TotalSeconds;
            }
        }

        public override Bullet Clone()
        {
            Bullet shotGunBullet = new ShotGunBullet();
            shotGunBullet.Copy(this);
            return shotGunBullet;
        }

        public override void Copy(Bullet Origin)
        {
            base.Copy(Origin);
        }
    }
}
